<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { Search, Plus, Download, Refresh } from '@element-plus/icons-vue';

/**
 * 此页面对应账单中心的 订单统计
 */
// 查询条件
const queryForm = reactive({
  waybillNo: '', // 运单号
  customer: '', // 客户
  startDate: '', // 开始日期
  endDate: '', // 结束日期
});

// 表格数据
const tableData = ref<any[]>([]);
// 表格加载状态
const loading = ref(false);
// 表格选中的行
const selectedRows = ref<any[]>([]);

// 分页参数
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
});

// 获取表格数据
const fetchTableData = async () => {
  loading.value = true;
  try {
    // 模拟数据
    const mockData = Array.from({ length: pagination.pageSize }, (_, index) => {
      const id = index + (pagination.currentPage - 1) * pagination.pageSize;
      return {
        id: `${39029034832 + id}`,
        waybillNo: `${39029034832 + id}`,
        customer: '宋微微',
        orderQuantity: 5,
        cargoWeight: 50,
        cargoVolume: 50,
        cargoQuantity: 50,
        totalAmount: 5000,
        status: id % 2 === 0 ? '未结算' : '已结算',
        createTime: '2023-10-15 14:30:00'
      };
    });

    tableData.value = mockData;
    pagination.total = 50; // 模拟总数据量
  } catch (error) {
    console.error('获取数据失败', error);
    ElMessage.error('获取数据失败');
  } finally {
    loading.value = false;
  }
};

// 重置查询条件
const resetQueryForm = () => {
  Object.keys(queryForm).forEach(key => {
    (queryForm as any)[key] = '';
  });
  handleSearch();
};

// 查询数据
const handleSearch = () => {
  pagination.currentPage = 1;
  fetchTableData();
};

// 处理表格选择变化
const handleSelectionChange = (rows: any[]) => {
  selectedRows.value = rows;
};

// 处理分页变化
const handlePageChange = (currentPage: number) => {
  pagination.currentPage = currentPage;
  fetchTableData();
};

// 处理每页条数变化
const handleSizeChange = (pageSize: number) => {
  pagination.pageSize = pageSize;
  pagination.currentPage = 1;
  fetchTableData();
};

// 导出数据
const exportData = () => {
  if (selectedRows.value.length === 0) {
    ElMessage.warning('请选择要导出的记录');
    return;
  }
  // 实现导出功能
  console.log('导出数据', selectedRows.value);
};

// 页面加载时获取数据
onMounted(() => {
  fetchTableData();
});
</script>

<template>
  <div class="waybill-manage-view">
    <div class="page-header">
      <h2 class="page-title">运单管理</h2>
    </div>

    <!-- 搜索区域 -->
    <div class="search-container">
      <el-form :model="queryForm" inline>
        <el-form-item label="运单号">
          <el-input v-model="queryForm.waybillNo" placeholder="请输入运单号" clearable></el-input>
        </el-form-item>

        <el-form-item label="客户">
          <el-input v-model="queryForm.customer" placeholder="请输入客户名称" clearable></el-input>
        </el-form-item>

        <el-form-item label="创建日期">
          <el-date-picker
            v-model="queryForm.startDate"
            type="date"
            placeholder="开始日期"
            style="width: 150px"
          />
          <span class="date-separator">至</span>
          <el-date-picker
            v-model="queryForm.endDate"
            type="date"
            placeholder="结束日期"
            style="width: 150px"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetQueryForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格工具栏 -->
    <div class="table-toolbar">
      <div class="left">
        <el-button type="primary" :icon="Plus">批量导入</el-button>
      </div>
      <div class="right">
        <el-button :icon="Download" @click="exportData">导出</el-button>
      </div>
    </div>

    <!-- 表格 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      @selection-change="handleSelectionChange"
      style="width: 100%"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="waybillNo" label="运单号" width="150" />
      <el-table-column prop="customer" label="客户" width="120" />
      <el-table-column prop="orderQuantity" label="订单数量" width="100" />
      <el-table-column prop="cargoWeight" label="货物重量(kg)" width="120" />
      <el-table-column prop="cargoVolume" label="货物体积(m³)" width="120" />
      <el-table-column prop="cargoQuantity" label="货物数量" width="100" />
      <el-table-column prop="totalAmount" label="总金额(元)" width="120" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="{ row }">
          <el-tag :type="row.status === '已结算' ? 'success' : 'warning'">
            {{ row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column label="操作" fixed="right" width="150">
        <template #default="{ row }">
          <el-button type="primary" link>结算</el-button>
          <el-button type="primary" link>明细</el-button>
          <el-button type="primary" link>打印</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="pagination.currentPage"
        v-model:page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="pagination.total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      />
    </div>
  </div>
</template>

<style scoped>
.waybill-manage-view {
  padding: 20px;
}

.page-header {
  margin-bottom: 20px;
}

.page-title {
  font-size: 20px;
  font-weight: 500;
  margin: 0;
}

.search-container {
  background-color: #fff;
  padding: 20px;
  border-radius: 4px;
  margin-bottom: 20px;
}

.date-separator {
  margin: 0 10px;
}

.table-toolbar {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>